<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>冒泡排序</title>
  </head>

  <body>
    <p>冒泡排序</p>
    <script>
      function bubbleSort(arr) {
        // i = arr.length - 1，这里进行了优化，每次遍历去掉上次最后已排序好的
        for (let i = arr.length - 1; i > 0; i--) {
          for (let j = 0; j < i; j++) {
            if (arr[j] > arr[j + 1]) {
              let temp = arr[j]
              arr[j] = arr[j + 1]
              arr[j + 1] = temp
            }
          }
        }
        return arr
      }
      console.log(bubbleSort([5, 1, 4, 2, 8, 3]))
    </script>
  </body>
</html>
